package com.encrypt;


import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

import java.io.UnsupportedEncodingException;

/**
 * Created by huangyugen on 2019/04/28
 * 广泛用于电子邮件
 **/
public class Base64Coder {

    public static final String ENCODING = "UTF-8";

    /**
     * Base64编码
     * @param data
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String encode(String data) {
        return  Base64.encode(data.getBytes());
    }

    /**
     * Base64解码
     * @param data
     * @return
     */
    public static String decode(String data) throws UnsupportedEncodingException {
        byte[] decode = Base64.decode(data);
        return new String(decode,ENCODING);
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String data = "Java加密与解密的艺术";
        System.out.println("【原 文】："+data);
        String encode = Base64Coder.encode(data);
        System.out.println("【编码后】："+encode);
        System.out.println("【解码后】："+Base64Coder.decode(encode));
    }

}
